﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Controllers
{
    public class MeetingRoomController : BaseController
    {
        //分页查询(会议室查询不分权限,按申请时间降序)
        public ActionResult Find(int start, int limit, string idStr)
        {
            int empId;
            IList<int> deptIdList = getAuthedDeptIds("Perchase", idStr, out empId);
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;

            IQueryable<MeetingRoomDTO> list = from r in ctx.ReqMeetingRooms
                                           join s in ctx.SignFlows on
                                               new { r.FormNo, IsCurrent = 1 } equals new { s.FormNo, s.IsCurrent }
                                               into t
                                              from x in t.DefaultIfEmpty()
                                              orderby r.PreStartTime descending
                                              select new MeetingRoomDTO
                                           {
                                               FormNo = r.FormNo,
                                               EmpId = r.EmpId,
                                               EmpName = r.Employee.Name,
                                               PreStartTime = r.PreStartTime,
                                               PreEndTime = r.PreEndTime,
                                               RoomName = r.RoomName,
                                               IsNeedProjector = r.IsNeedProjector,
                                               Cause = r.Cause,
                                               StatusId = r.StatusId,
                                               CheckEmpId = x.CheckEmpId
                                           };
            if (deptIdList != null && deptIdList.Count>0&&!string.IsNullOrWhiteSpace(idStr))
            {
                list = list.Where(r => (from r1 in ctx.EmpDepPositions where deptIdList.ToArray().Contains(r1.DeptId) select r1.EmpId).Contains(r.EmpId));
            }
            //IQueryable<ReqVehicleMains> list = from r in ctx.ReqVehicleMains select r;
            CJson CJson = new CJson();
            string json = CJson.ToJsonString("totalCount:" + list.Count() + ",data", list.Skip(start).Take(limit).ToList());
            return Content(json);
        }

        //删除申请
        public ActionResult Delete(string formNo)
        {
            var rdto = new ResultMessages();
            rdto.Message = "";
            ReqMeetingRoom oriObj = ctx.ReqMeetingRooms.Single(r => r.FormNo == formNo);
            ctx.ReqMeetingRooms.DeleteOnSubmit(oriObj);
            try
            {
                ctx.SubmitChanges();
                rdto.Message = "删除成功";
                rdto.Result = true;
            }
            catch
            {
                rdto.Message = "删除失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}